#include "../mqserver/Host.hpp"
#include <gtest/gtest.h>
#include <iostream>
#include <unordered_map>
using namespace std;
mq::VirtualHost::VirtualHostPtr vhp;
class HostTest : public testing::Environment
{
public:
    virtual void SetUp() override
    {
        vhp = make_shared<mq::VirtualHost>("host1", "./data/queues/", "./data/test.db");
        cout << "测试环境初始化" << endl;
    }
    virtual void TearDown() override
    {
        // vhp->clear();
        cout << "测试环境清理" << endl;
    }
};
// TEST(message, insert_test)
// {
//     unordered_map<string, string> args({{"k1", "v1"}, {"k2", "v2"}});
//     vhp->declareExchange("exchange1", mq::ExchangeType::DIRECT, true, true, args);
//     vhp->declareExchange("exchange2", mq::ExchangeType::TOPIC, true, true, args);

//     vhp->declareQueue("queue1", true, true, true, args);
//     vhp->declareQueue("queue2", false, true, true, args);
//     vhp->declareQueue("queue3", true, true, true, args);

//     vhp->bind("exchange1", "queue1", "sport");
//     vhp->bind("exchange1", "queue2", "study");

//     vhp->bind("exchange2", "queue2", "study");
//     vhp->bind("exchange2", "queue3", "music");

//     vhp->basicPublish("queue1", nullptr, "HelloWorld-1");
//     vhp->basicPublish("queue1", nullptr, "HelloWorld-2");

//     vhp->basicPublish("queue2", nullptr, "HelloWorld-3");
//     vhp->basicPublish("queue2", nullptr, "HelloWorld-4");

//     vhp->basicPublish("queue3", nullptr, "HelloWorld-5");
//     vhp->basicPublish("queue3", nullptr, "HelloWorld-6");
// }
// TEST(exchange, select_test)
// {
//     ASSERT_EQ(vhp->existsExchange("exchange1"), true);
//     ASSERT_EQ(vhp->existsExchange("exchange2"), true);

//     ASSERT_EQ(vhp->selectExchange("exchange1")->_name.c_str(), string("exchange1"));
//     ASSERT_EQ(vhp->selectExchange("exchange2")->_name.c_str(), string("exchange2"));

//     ASSERT_EQ(vhp->existsQueue("queue1"), true);
//     ASSERT_EQ(vhp->existsQueue("queue2"), true);
//     ASSERT_EQ(vhp->existsQueue("queue3"), true);

//     ASSERT_EQ(vhp->existsBinding("exchange1", "queue1"), true);
//     ASSERT_EQ(vhp->existsBinding("exchange1", "queue2"), true);
//     ASSERT_EQ(vhp->existsBinding("exchange1", "queue3"), false);
//     ASSERT_EQ(vhp->existsBinding("exchange2", "queue1"), false);
//     ASSERT_EQ(vhp->existsBinding("exchange2", "queue2"), true);
//     ASSERT_EQ(vhp->existsBinding("exchange2", "queue3"), true);

//     mq::BindingManager::MsgQueueBindingMap mp1 = vhp->exchangeBindings("exchange1");
//     mq::BindingManager::MsgQueueBindingMap mp2 = vhp->exchangeBindings("exchange2");
//     ASSERT_EQ(mp1.size(), 2);
//     ASSERT_EQ(mp2.size(), 2);
// }
// TEST(exchange, ack_test)
// {
//     auto msg = vhp->basicConsume("queue1");
//     ASSERT_EQ(msg->payload().body().c_str(), string("HelloWorld-1"));
//     msg = vhp->basicConsume("queue1");
//     ASSERT_EQ(msg->payload().body().c_str(), string("HelloWorld-2"));

//     msg = vhp->basicConsume("queue2");
//     ASSERT_EQ(msg->payload().body().c_str(), string("HelloWorld-3"));
//     msg = vhp->basicConsume("queue2");
//     ASSERT_EQ(msg->payload().body().c_str(), string("HelloWorld-4"));

//     msg = vhp->basicConsume("queue3");
//     ASSERT_EQ(msg->payload().body().c_str(), string("HelloWorld-5"));
//     msg = vhp->basicConsume("queue3");
//     ASSERT_EQ(msg->payload().body().c_str(), string("HelloWorld-6"));
// }
// TEST(exchange, delete_test)
// {
//     vhp->unBind("exchange1", "queue2");
//     vhp->unBind("exchange2", "queue2");
//     mq::BindingManager::MsgQueueBindingMap mp1 = vhp->exchangeBindings("exchange1");
//     mq::BindingManager::MsgQueueBindingMap mp2 = vhp->exchangeBindings("exchange2");
//     ASSERT_EQ(mp1.size(), 1);
//     ASSERT_EQ(mp2.size(), 1);

//     vhp->deleteExchange("exchange2");
//     ASSERT_EQ(vhp->existsExchange("exchange2"), false);
//     vhp->deleteQueue("queue2");
//     ASSERT_EQ(vhp->existsQueue("queue1"), true);
//     ASSERT_EQ(vhp->existsQueue("queue2"), false);
//     ASSERT_EQ(vhp->existsQueue("queue3"), true);
// }
TEST(message, recovery_test)
{
    ASSERT_EQ(vhp->existsExchange("exchange1"), true);
    ASSERT_EQ(vhp->existsExchange("exchange2"), true);

    ASSERT_EQ(vhp->existsQueue("queue1"), true);
    ASSERT_EQ(vhp->existsQueue("queue2"), false);
    ASSERT_EQ(vhp->existsQueue("queue3"), true);

    ASSERT_EQ(vhp->existsBinding("exchange1", "queue1"), true);
    ASSERT_EQ(vhp->existsBinding("exchange1", "queue2"), false);
    ASSERT_EQ(vhp->existsBinding("exchange1", "queue3"), false);
    ASSERT_EQ(vhp->existsBinding("exchange2", "queue1"), false);
    ASSERT_EQ(vhp->existsBinding("exchange2", "queue2"), false);
    ASSERT_EQ(vhp->existsBinding("exchange2", "queue3"), true);

    mq::BindingManager::MsgQueueBindingMap mp1 = vhp->exchangeBindings("exchange1");
    mq::BindingManager::MsgQueueBindingMap mp2 = vhp->exchangeBindings("exchange2");
    ASSERT_EQ(mp1.size(), 1);
    ASSERT_EQ(mp2.size(), 1);

    auto msg = vhp->basicConsume("queue1");
    ASSERT_EQ(msg->payload().body().c_str(), string("HelloWorld-1"));
    msg = vhp->basicConsume("queue1");
    ASSERT_EQ(msg->payload().body().c_str(), string("HelloWorld-2"));

    msg = vhp->basicConsume("queue2");
    ASSERT_EQ(msg.get(), nullptr);
    msg = vhp->basicConsume("queue2");
    ASSERT_EQ(msg.get(), nullptr);

    msg = vhp->basicConsume("queue3");
    ASSERT_EQ(msg->payload().body().c_str(), string("HelloWorld-5"));
    msg = vhp->basicConsume("queue3");
    ASSERT_EQ(msg->payload().body().c_str(), string("HelloWorld-6"));
}
int main(int argc, char *argv[])
{
    testing::AddGlobalTestEnvironment(new HostTest);
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}
